<?php

namespace App\Http\Controllers\Web;

use App\Models\BuildType;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BuildTypeController extends Controller
{
    public function index(Request $request)
    {

        $size = $request->input('size', 10);
        $query = BuildType::query()->with('product');

        return $this->successPaginate($query->paginate($size));
    }

    public function requestData(Request $request)
    {
        $requestData = $this->validate($request, [
            'name' => 'required',
            'name_en' => 'nullable',
            'product_id' => 'required',
        ]);
        return $requestData;
    }

    public function show($id)
    {
        $new = BuildType::query()->findOrFail($id);
        return $this->success($new);
    }

    public function store(Request $request)
    {
        $new = new BuildType;
        $requestData = $this->requestData($request);
        $new->fill($requestData);
        $new->save();
        return $this->success($requestData);
    }

    public function update(Request $request, $id)
    {
        $new = BuildType::query()->findOrFail($id);
        $requestData = $this->requestData($request);
        $new->fill($requestData);
        $new->save();
        return $this->success($requestData);
    }

    public function destroy($id)
    {
        $new = BuildType::query()->findOrFail($id);
        $new->delete();
    }
}
